<?php

if_get('/subjects', function ()
{/*{{{*/
    list(
        $inputs['name'], $inputs['type']
    ) = input_list(
        'name', 'type'
    );

    $inputs = array_filter($inputs, 'not_null');

    $subjects = dao('subject')->find_all_by_column($inputs);

    return [
        'count' => count($subjects),
        'subjects' => array_build($subjects, function ($id, $subject) {
            return [
                null,
                [
                    'id' => $subject->id,
                    'name' => $subject->name,
                    'type' => $subject->get_type_description(),
                    'create_time' => $subject->create_time,
                    'update_time' => $subject->update_time,
                ]
            ];
        }),
    ];
});/*}}}*/

if_post('/subjects/add', function ()
{/*{{{*/
    $name = input('name');
    otherwise_error_code('SUBJECT_REQUIRE_NAME', $name);
    $type = input('type');
    otherwise_error_code('SUBJECT_REQUIRE_TYPE', $type);

    $another_subject = dao('subject')->find_by_name_and_type($name, $type);
    otherwise_error_code('SUBJECT_DUPLICATED', $another_subject->is_null(), [':subject_id' => $another_subject->id]);

    $subject = subject::create(
        $name,
        $type
    );

    $field_infos = input('field_infos');

    otherwise_error_code('SUBJECT_REQUIRE_FIELD_INFOS', $field_infos);

    foreach ($field_infos as $field_info) {
        otherwise_error_code('FIELD_REQUIRE_NAME', isset($field_info['name']));
        otherwise_error_code('FIELD_REQUIRE_KEY', isset($field_info['key']));
        otherwise_error_code('FIELD_REQUIRE_TYPE', isset($field_info['type']));

        $field = field::create(
            $subject,
            $field_info['name'],
            $field_info['key'],
            $field_info['type']
        );
    }

    return [
        'id' => $subject->id,
    ];
});/*}}}*/

if_get('/subjects/*', function ($subject_id)
{/*{{{*/
    $subject = dao('subject')->find($subject_id);
    otherwise_error_code('SUBJECT_NOT_FOUND', $subject->is_not_null());

    return [
        'count' => count($subjects),
        'subject' => $subject,
        'fields' => array_build($subject->fields, function ($id, $field) {
            return [
                null,
                [
                    'id' => $field->id,
                    'name' => $field->name,
                    'key' => $field->key,
                    'type' => $field->get_type_description(),
                    'subject_display' => $field->subject->display_for_fields_subject(),
                    'create_time' => $field->create_time,
                    'update_time' => $field->update_time,
                ]
            ];
        }),
    ];
});/*}}}*/

if_post('/subjects/update/*', function ($subject_id)
{/*{{{*/
    $name = input('name');
    $type = input('type');

    $subject = dao('subject')->find($subject_id);
    otherwise_error_code('SUBJECT_NOT_FOUND', $subject->is_not_null());

    $another_subject = dao('subject')->find_by_name_and_type($name, $type);
    otherwise_error_code('SUBJECT_DUPLICATED', $another_subject->is_null() || $another_subject->id === $subject->id, [':subject_id' => $another_subject->id]);

    $subject->name = $name;
    $subject->type = $type;

    return true;
});/*}}}*/

if_post('/subjects/delete/*', function ($subject_id)
{/*{{{*/
    $subject = dao('subject')->find($subject_id);
    otherwise_error_code('SUBJECT_NOT_FOUND', $subject->is_not_null());

    $subject->delete();

    return true;
});/*}}}*/
